package org.jflame.commons.file;

import org.jflame.commons.util.StringHelper;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;

public abstract class FileFilters {

    /**
     * 文件过滤器-&gt;非目录文件
     */
    public static FileFilter JUST_FILE_FILTER = new FileFilter() {

        @Override
        public boolean accept(File pathname) {
            return pathname.isFile();
        }
    };

    /**
     * 文件名过滤器-&gt;非目录文件
     */
    public static FilenameFilter JUST_FILE_NAMEFILTER = new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            File f = new File(dir, name);
            return f.isFile() && !f.isHidden();
        }
    };

    /**
     * 文件过滤器-&gt;按文件扩展名过滤(扩展名包含.且小写)
     * 
     * @author charles.zhang
     */
    public static class SuffixFileFilter implements FileFilter {

        private final String[] suffixes;

        public SuffixFileFilter(String[] suffixes) {
            this.suffixes = suffixes;
        }

        @Override
        public boolean accept(File pathname) {
            return pathname.isFile() && StringHelper.endsWithAny(pathname.getName()
                    .toLowerCase(), suffixes);
        }
    }

}
